package com.bw.zonghe0719.Login

import android.content.Intent
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.mvvm.BaseActivity
import com.bw.zonghe0719.R
import com.bw.zonghe0719.databinding.ActivityLoginPwdBinding
@Route(path = "/app/Login/LoginPwdActivity")
class LoginPwdActivity :BaseActivity<ActivityLoginPwdBinding,LoginViewModel>(){
    override fun initView() {
        binding.login.setOnClickListener {
            var phone=binding.phone.text.toString()
            var pwd=binding.pwd.text.toString()
            if(pwd.isEmpty()||phone.isEmpty()){
                ToastUtils.showLong("输入框不能为空")
            }else{
                if(RegexUtils.isMobileExact(phone)){
                    viewModel.loginPwd(phone,pwd)
                }else{
                    ToastUtils.showLong("用户名格式错误")
                }
            }
        }
    }

    override fun initData() {
        viewModel.loginPwdSuccess.observe(this){
            if(it.code==200){
                SPUtils.getInstance().apply {
                    put("token",it.data.token)
                    put("phone",it.data.phone)
                }
                startActivity(Intent(this@LoginPwdActivity,UserActivity::class.java))
            }
            ToastUtils.showLong(it.message)
        }
    }

    override fun getViewModel(): Class<LoginViewModel> {
        return LoginViewModel::class.java
    }

    override fun getLayoutId(): Int {
       return R.layout.activity_login_pwd
    }
}